const multer = require('@koa/multer');
const path = require("path");
// fileName 文件名，默认为文件字段名 + 时间戳
// filePath 文件存放路径,必填
function uploads(filePath, fileName) {

  if (filePath) {
    const storage = multer.diskStorage({
      destination: function (req, file, cb) {
        cb(null, path.join(__dirname, `../${filePath}`));
      },
      filename: function (req, file, cb) {
        const defaultFileName = fileName ? fileName : file.fieldname;
        const suffix = path.extname(file.originalname)
        cb(null, `${defaultFileName}-${Date.now()}${suffix}`);
      }
    })
    return upload = multer({
      storage: storage
    })
  } else {
    throw new Error('上传文件的存放路径未设置！');
  }

}
module.exports = uploads